<?php

namespace app\models;
use app\models\GkyUser;
use Yii;

/**
 * This is the model class for table "gky_post".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $content
 * @property string $createtime
 */
class GkyPost extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_post';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'content', 'createtime'], 'required'],
            [['user_id'], 'integer'],
            [['createtime'], 'safe'],
            [['content'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'content' => 'Content',
            'createtime' => 'Createtime',
        ];
    }
    
    public function getPost(){

        return $this->hasOne(GkyUser::className(),['id'=>'user_id']);
    }
    /**
     * 获取自己发的帖子的list
     * @param type $userid
     * @param type $pageNow
     * @param type $pageSize
     * @return type
     */
    public static function findPostList($pageNow, $pageSize){
         $list = GkyPost::find()
            ->orderBy(['createtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
         
         $results = [];
        foreach($list as $model){
            $result['id'] = $model->id;
            $result['user_id'] = $model->user_id;
            $result['username'] = GkyWeFans::find()->where(['id'=>$model->user_id])->one()->nickname;
            $result['content'] = $model->content;
            $result['createtime'] = $model->createtime;
            $results[] = $result;
        }
        return $results;
    }
}
